<?php
/**
 * Created by PhpStorm.
 * User: ZhangWei
 * Date: 2018/2/23
 * Time: 14:06
 */

namespace App\Http\Controllers\Api\V1;


use App\Http\Controllers\Api\ApiController;
use App\Http\Resources\User as UserResources;
use App\Models\MoneyLog;
use App\Models\Withdraw;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class WalletController extends BaseController
{
    public function index()
    {
        $user = $this->getUser();

        $data['user'] = new UserResources($user);

        $withdraw_min = config('withdraw_min', 5);

        $data['desc'] = [
            [
                'color' => '#ffffff',
                'text' => '1.提现需要审核，请注意查看'
            ],
            [
                'color' => '',
                'text' => '2.满' . $withdraw_min . '元后可提现'
            ],
            [
                'color' => '',
                'text' => '3.每次不超过2000元'
            ]
        ];

        $data['more'] = [
            /*[
                'title'=>'分销课程获取更多奖励',
                'info'=>'1.生成邀请卡分享给好友<br>2.好友购买课程即可获得奖励',
                'color'=>'#ffffff',
                'route'=>[
                    'title'=>'立即分销',
                    'name'=>'bazaar',
                    'query'=>[],
                    'color'=>['#19be6b','#19be6b']
                ]
            ]*/
        ];

        $data['config'] = [
            'withdraw_min' => $withdraw_min
        ];

        return $this->success($data);
    }

    public function wallet_log()
    {
        $user = $this->getUser();

        $log = MoneyLog::query()->where('user_id', $user->id)->limit(200);

        $log->orderByDesc('id');
        $list = $log->get();
        $data['list'] = \App\Http\Resources\MoneyLog::collection($list);
        return $this->success($data);
    }


    public function withdraw(Request $request)
    {
        $user = $this->getUser();
        $money = $request->input("withdraw");
        $withdraw_min = config('withdraw_min', 5);
        $validator = Validator::make($request->all(), [
            'withdraw' => "required|min:$withdraw_min|max:2000|numeric",

        ], [
            'withdraw.required' => '请输入提现金额',
            'withdraw.min' => '最少提现' . $withdraw_min . '元',
            'withdraw.max' => '最多提现2000元',
            'withdraw.numeric' => '提现金额错误',
        ]);
        if ($validator->fails()) {
            return $this->failed($validator->errors()->first());
        }


        $wd = Withdraw::addWithdraw($user, $money, Withdraw::WX, config('wx.mch_id'), $user->wx_user->open_id);
        if ($wd) {
            return $this->message('提现成功');
        } else {
            return $this->failed('提现失败,请稍候再试');
        }

    }

    public function withdraw_log(){
        $user = $this->getUser();

        $log = Withdraw::query()->where('user_id', $user->id);

        $log->orderByDesc('id');
        $list = $log->get();
        $data['list'] = \App\Http\Resources\Withdraw::collection($list);
        return $this->success($data);
    }

}